package com.naizhuai.ui.ainote.notesetting;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naizhuai.Model.ModelProvider;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddDetialNoteActivity extends BaseActivity implements AddDetialContract.View {
    public static final String NOTE_DETIAL_ID = "note_detial_id";

    AddDetialContract.Presenter mPresenter;
    @BindView(R.id.saveToolbar)
    Toolbar saveToolbar;
    @BindView(R.id.detail_title)
    EditText detailTitle;
    @BindView(R.id.creating)
    TextView creating;
    @BindView(R.id.last_update)
    TextView lastUpdate;
    @BindView(R.id.timestamp)
    LinearLayout timestamp;
    @BindView(R.id.detail_content)
    EditText detailContent;
    @BindView(R.id.rootlayout)
    LinearLayout rootlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        InitToolBar();
        InitPresenter();
    }

    private void InitPresenter() {
        int note_id = getIntent().getIntExtra(NOTE_DETIAL_ID, -1);
        mPresenter = new AddDetialPresenter(note_id, ModelProvider.provideNotesRepository());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.takeView(this);
        detailTitle.requestFocus(1000);
    }

    private void InitToolBar() {
        saveToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = detailTitle.getText().toString().trim();
                String detail = detailContent.getText().toString().trim();
                mPresenter.saveTask(title, detail);
            }
        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_add_detial_note;
    }

    @Override
    public void showEmptyNoteError() {
        Snackbar.make(rootlayout,
                getString(R.string.note_not_null),
                Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void showTasksList() {
        finish();
    }

    @Override
    public void setTitle(String title) {
        detailTitle.setText(title);
    }

    @Override
    public void setDescription(String description) {
        detailContent.setText(description);
    }

    @Override
    public void setCreation(String creation) {
        creating.setText(creation);
    }

    @Override
    public void setLastupdate(String lastupdate) {
        lastUpdate.setText(lastupdate);
    }

    @Override
    public void showTimeStamp() {
        timestamp.setVisibility(View.VISIBLE);
    }
}
